/****************************************************************************
**
** Copyright (C) 2009 by user *
** user@greenphonesdk *
**
** Contact: Qt Extended Information (info@qtextended.org)
**
** This file may be used under the terms of the GNU General Public License
** version 2.0 as published by the Free Software Foundation and appearing
** in the file LICENSE.GPL included in the packaging of this file.
**
** Please review the following information to ensure GNU General Public
** Licensing requirements will be met:
**     http://www.fsf.org/licensing/licenses/info/GPLv2.html.
**
****************************************************************************/

#include "addcontactdialog.h"
#include <QImageDocumentSelectorDialog>

AddContactDialog::AddContactDialog(QWidget *parent, Qt::WFlags f)
    : QWidget(parent, f)
{
    // initialize the dialog
	setupUi(this);	
	entry = NULL;
}

AddContactDialog::~AddContactDialog()
{
    // Do nothing
}

void AddContactDialog::setupEdit(NeoPhoneBookEntry *entryToEdit)
{
    entry = entryToEdit;
	nameLineEdit->setText(entryToEdit->getContactName());
	numberLineEdit->setText(entryToEdit->getPhoneNumber());
	emailLineEdit->setText(entryToEdit->getContactEmail());
	pictureFilePath = entryToEdit->getPictureFilePath();
	if (!pictureFilePath.isEmpty()) {
		QPixmap pixmap(pictureFilePath);
		pixmap = pixmap.scaled(QSize(pictureLabel->width(), pictureLabel->height())
				, Qt::KeepAspectRatio, Qt::SmoothTransformation);
		pictureLabel->setPixmap(pixmap);
		removePushButton->setEnabled(true);
	}
}


void AddContactDialog::on_savePushButton_clicked() {
	QString name = nameLineEdit->text();
	QString number = numberLineEdit->text();
	QString email = emailLineEdit->text();
	
	if (name.isEmpty() || number.isEmpty()) {
		QMessageBox::warning(this, "Missing Info.",
				"Name and phone number are required.", QMessageBox::Ok);
	} else {
		if (entry == NULL) { // new contact
			entry = new NeoPhoneBookEntry(name, number);
			entry->setContactEmail(email);
			entry->setPictureFilePath(pictureFilePath);
		} else {
			entry->setContactName(name);
			entry->setPhoneNumber(number);
			entry->setContactEmail(email);
			entry->setPictureFilePath(pictureFilePath);
		}
		emit updateContactSignal(entry);
		close();
	}
}
void AddContactDialog::on_cancelPushButton_clicked() 
{
	close();
}

void AddContactDialog::on_removePushButton_clicked() 
{
	pictureFilePath = "";
	pictureLabel->setText("No Picture");
	removePushButton->setEnabled(false);
}
void AddContactDialog::on_browsePushButton_clicked() 
{
	QImageDocumentSelectorDialog dialog(this);
	
	if (QtopiaApplication::execDialog(&dialog)) {
		QContent content = dialog.selectedDocument();
		QPixmap pixmap = QPixmap(content.fileName());

		QFileInfo selectedFile(content.fileName());
		pictureFilePath = selectedFile.filePath();
		// absoluteFilePath();
		if (!pixmap.isNull()) {
			pixmap = pixmap.scaled(QSize(pictureLabel->width(), 
					 pictureLabel->height()), Qt::KeepAspectRatio,
					 Qt::SmoothTransformation);
			pictureLabel->setPixmap(pixmap);
			removePushButton->setEnabled(true);
		}
	}
}

